﻿using Customer.Read.API.Dtos;
using Customer.Domain;
using Customer.Read.API.Applcation.Command.VendorCommand;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;

namespace Customer.Read.API.Controllers
{
    /// <summary>
    /// 供应商显示控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class VendorController : ControllerBase
    {
        private readonly IMediator _IMediator;
        public VendorController(IMediator _IMediator)
        {
            this._IMediator = _IMediator;
        }
        /// <summary>
        /// 供应商显示
        /// </summary>
        /// <param name="command">查询条件</param>
        /// <returns>返回满足条件的数据</returns>
        [HttpGet]
        public async Task<ApiResult<Vendorpage>> GetShowVendor([FromQuery] SelectVendorCommand command)
        {
            ApiResult<Vendorpage> result = new ApiResult<Vendorpage>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                result.Data = await _IMediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 供应商下拉框
        /// </summary>
        /// <returns>返回满足条件的数据</returns>
        [HttpGet]
        public async Task<ApiResult<List<Vendor>>> GetDropVendor()
        {
            ApiResult<List<Vendor>> result = new ApiResult<List<Vendor>>();
            try
            {
                result.Data = await _IMediator.Send(new VendorDropCommand());
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填供应商
        /// </summary>
        /// <param name="command">要反填的id</param>
        /// <returns>返回满足条件的数据</returns>
        [HttpGet]
        public async Task<ApiResult<Vendor>> GetVendorById([FromQuery] VendorFanCommand command)
        {
            ApiResult<Vendor> result = new ApiResult<Vendor>();
            try
            {
                result.Data = await _IMediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 供应商信息表导出
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FileContentResult> GetAllVendorExel([FromQuery] SelectVendorCommand command)
        {
            var obj = await _IMediator.Send(command);
            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("供应商信息表");
            IRow row;
            row = sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("供应商编号");
            row.CreateCell(1).SetCellValue("供应商名称");
            row.CreateCell(2).SetCellValue("类型");
            row.CreateCell(3).SetCellValue("电话");
            row.CreateCell(4).SetCellValue("传真");
            row.CreateCell(5).SetCellValue("Email");
            row.CreateCell(6).SetCellValue("联系人");
            row.CreateCell(7).SetCellValue("地址");
            row.CreateCell(7).SetCellValue("创建时间");
            int index = 1;
            foreach (var item in obj.list)
            {
                row = sheet.CreateRow(index);
                row.CreateCell(0).SetCellValue(item.VendorCode);
                row.CreateCell(1).SetCellValue(item.VendorName);
                row.CreateCell(2).SetCellValue(item.VendorType);
                row.CreateCell(3).SetCellValue(item.Vphone);
                row.CreateCell(4).SetCellValue(item.Vfax);
                row.CreateCell(5).SetCellValue(item.Email);
                row.CreateCell(6).SetCellValue(item.Vcontact);
                row.CreateCell(7).SetCellValue(item.Vaddress);
                row.CreateCell(7).SetCellValue((DateTime)item.CreatTime);
                index++;
            }
            byte[] buffe;
            using(MemoryStream ms=new MemoryStream())
            {
                wb.Write(ms);
                buffe = ms.ToArray();
            }
            string dslist = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(buffe, dslist, "供应商信息表.xlsx");
        }
    }
}
